export default class DCMSeries {
	constructor(args) {
		this.imageList = [];
		this.study = null;
		this.seriesInsUid = "";
		this.seriesNumber = "";
		this.seriesDesc = "";
		this.seriesDate = null;
		this.seriesTime = null;
		this.thumb = null;

		if (args) {
			this.study = args.study;
			this.seriesInsUid = args.seriesInsUid;
			this.seriesNumber = args.seriesNumber;
			this.seriesDesc = args.seriesDesc;
			this.seriesDate = args.seriesDate;
			this.seriesTime = args.seriesTime;
		}
	}

	AddImage(dcmImg, sort = true) {
		if (!dcmImg) return;
		const existImg = this.imageList.find(
			item => item.imageId === dcmImg.imageId
		);
		if (!existImg) {
			dcmImg.series = this;
			this.imageList.push(dcmImg);
			if (sort) {
				this.Sort();
			}
		}
	}

	Sort(bAsc = true) {
		this.imageList.sort(function (a, b) {
			if (
				a.AcquisitionTime &&
				b.AcquisitionTime &&
				a.AcquisitionTime !== b.AcquisitionTime
			) {
				const dateStrA =
					a.AcquisitionDate + "T" + a.AcquisitionTime + "Z";
				const dateStrB =
					b.AcquisitionDate + "T" + b.AcquisitionTime + "Z";
				const s1 = Date.parse(dateStrA);
				const s2 = Date.parse(dateStrB);
				// console.log(
				// 	"series sort dateStrA: ",
				// 	dateStrA,
				// 	" dateStrB: ",
				// 	dateStrB
				// );
				let val;
				val = bAsc ? s1 - s2 : s2 - s1;
				return val; // 升序
			} else {
				const s1 = parseInt(a.instanceNumber);
				const s2 = parseInt(b.instanceNumber);
				let val;
				val = bAsc ? s1 - s2 : s2 - s1;
				return val; // 升序
			}
		});
	}

	async GetThumb() {
		if (this.thumb) {
			return this.thumb;
		} else {
			if (this.imageList.length > 0) {
				const img = this.imageList[0];
				this.thumb = await img.createThumb("png");
				return this.thumb;
			} else {
				return null;
			}
		}
	}

	GetCount() {
		return this.imageList.length;
	}

	GetImageByIndex(idx) {
		if (idx < 0 || idx >= this.imageList.length) return null;
		return this.imageList[idx];
	}

	GetImageById(imageId) {
		// const img = this.imageList.find(item => item.imageId === imageId);
		const img = this.imageList.find(item => imageId.includes(item.imageId));
		return img;
	}

	GetImageIds() {
		let imageIds = [];
		for (let i = 0; i < this.imageList.length; i++) {
			imageIds.push(this.imageList[i].imageId);
		}
		return imageIds;
	}
}
